﻿// <copyright file="Imaging.cs" company="Microsoft Corporation">
// Copyright (c) 2008 All Right Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2008-10-10</date>
// <summary>Imaging class</summary>

namespace Synergist.WordToXamlLibrary
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Media.Imaging;

    /// <summary>
    /// Imaging class to use during XSL transformation
    /// </summary>
    internal class Imaging
    {
        #region Fields
        /// <summary>
        /// Directory for destination files
        /// </summary>
        private string directory;

        /// <summary>
        /// Resource distionary
        /// </summary>
        private System.Windows.ResourceDictionary resources;
        #endregion

        /// <summary>
        /// Initializes a new instance of the Imaging class.
        /// </summary>
        /// <param name="directory">the directory to put files into</param>
        internal Imaging(string directory)
        {
            this.directory = directory;
        }

        /// <summary>
        /// Initializes a new instance of the Imaging class.
        /// </summary>
        /// <param name="resources">the resources</param>
        internal Imaging(System.Windows.ResourceDictionary resources)
        {
            this.resources = resources;
        }

        /// <summary>
        /// Copy an image from the source filename to the resources or the output directory
        /// </summary>
        /// <param name="fileName">the source filename</param>
        /// <returns>the dynamic resource name or the local filename of the image</returns>
        public string CopyImage(string fileName)
        {
            if (fileName.StartsWith("file:///"))
            {
                fileName = fileName.Substring(8);
            }

            if (!System.IO.File.Exists(fileName))
            {
                return string.Empty;
            }

            string key = System.IO.Path.GetFileName(fileName);

            if (string.IsNullOrEmpty(this.directory))
            {
                BitmapImage bitmapImage = new BitmapImage(new Uri(fileName));

                this.resources.Add(key, bitmapImage);

                return "{DynamicResource " + key + "}";
            }
            else
            {
                string fullPath = this.directory + "\\" + key;

                string directory = System.IO.Path.GetDirectoryName(fullPath);

                if (!System.IO.Directory.Exists(directory))
                {
                    System.IO.Directory.CreateDirectory(directory);
                }

                System.IO.File.Copy(fileName, fullPath, true);

                return key;
            }
        }

        /// <summary>
        /// If we are saving to a file, then create the image in the directory otherwise add as a BitmapImage resource
        /// </summary>
        /// <param name="fileName">the filename or resource key</param>
        /// <param name="base64">a Base-encoded image</param>
        /// <returns>the source name for the image</returns>
        /// <remarks>Ignore Code Analysis warning AvoidUncalledPrivateCode</remarks>
        public string SaveImage(string fileName, string base64)
        {
            byte[] data = System.Convert.FromBase64String(base64);

            using (System.IO.MemoryStream stream = new System.IO.MemoryStream(data))
            {
                if (string.IsNullOrEmpty(this.directory))
                {
                    BitmapImage bitmapImage = new BitmapImage();

                    bitmapImage.BeginInit();
                    bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                    bitmapImage.StreamSource = stream;
                    bitmapImage.EndInit();

                    this.resources.Add(fileName, bitmapImage);

                    return "{DynamicResource " + fileName + "}";
                }
                else
                {
                    using (Image image = Image.FromStream(stream))
                    {
                        string fullPath = this.directory + "\\" + fileName.Replace('/', '\\');

                        string destination = System.IO.Path.GetDirectoryName(fullPath);
                        if (!System.IO.Directory.Exists(destination))
                        {
                            System.IO.Directory.CreateDirectory(destination);
                        }

                        image.Save(fullPath);

                        return fileName;
                    }
                }
            }
        }
    }
}
